public class Solution {
    /* https://leetcode-cn.com/problems/number-of-islands/submissions/
     * 岛屿数量
     *
     * */
    public void DFSlands(char[][] grid, int row, int col, int newR, int newC) {
        if (newR >= row || newR < 0 || newC >= col || newC < 0) {
            return;
        }
        if (grid[newR][newC] != '1') {
            return;
        }
        grid[newR][newC] = '0';
        for (int[] next : nextP) {
            int newX = newR + next[0];
            int newY = newC + next[1];
            DFSlands(grid, row, col, newX, newY);
        }
    }

    public int numIslands(char[][] grid) {
        int count = 0;
        int row = grid.length;
        int col = grid[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    DFSlands(grid, row, col, i, j);
                }
            }
        }
        return count;
    }

    /*
     * 标记数组辅助 int[][]book 代表某处 是否已经搜索过了
     *
     * */
    public void DFSlands2(char[][] grid, int row, int col, int newR, int newC, boolean[][] book) {
        if (newR >= row || newR < 0 || newC >= col || newC < 0) {
            return;
        }
        if (grid[newR][newC] != '1') {
            return;
        }
        book[newR][newC] = true;
        for (int[] next : nextP) {
            int newX = newR + next[0];
            int newY = newC + next[1];
            DFSlands2(grid, row, col, newX, newY, book);
        }
    }

    public int numIslands2(char[][] grid) {
        int count = 0;
        int row = grid.length;
        int col = grid[0].length;
        boolean[][] book = new boolean[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1' && !book[i][j]) {
                    count++;
                    DFSlands2(grid, row, col, i, j, book);
                }
            }
        }
        return count;
    }
}